import 'package:flutter/material.dart';
import 'package:get/route_manager.dart';
import 'package:tiktokadult/assets_tool/images.dart';
import 'package:tiktokadult/hj_page/mine_business/mine_gesture_pwd/widget/gesture_password_widget.dart';
import 'package:tiktokadult/hj_page/splash/splash_page.dart';
import 'package:tiktokadult/hj_utils/global.dart';
import 'package:tiktokadult/hj_utils/widget_utils.dart';
import 'package:tiktokadult/tools_base/global_store/store.dart';

enum MinePasswordType {
  setting,
  close,
  check,
}

class MinePasswordPage extends StatefulWidget {
  static const routeNmae = '/mine/password';
  const MinePasswordPage({super.key});

  @override
  State<MinePasswordPage> createState() => _MinePasswordPageState();
}

class _MinePasswordPageState extends State<MinePasswordPage> {
  late MinePasswordType type;
  @override
  void initState() {
    super.initState();
    type = Get.arguments ?? MinePasswordType.setting;
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: type != MinePasswordType.check
          ? AppBar(
              title: Text('手势密码'),
            )
          : null,
      body: Column(
        crossAxisAlignment: CrossAxisAlignment.center,
        children: [
          if (type == MinePasswordType.check) ...[
            65.sizeBoxH,
            Image.asset(
              'mine_lock_password.png'.mineImgPath,
              width: 29,
            ),
          ],
          55.sizeBoxH,
          Text(
            _generateTitle(),
            style: TextStyle(color: Colors.black.withOpacity(0.9), fontSize: 18),
          ),
          75.sizeBoxH,
          Center(
            child: GesturePasswordWidget(
              // color: Colors.green,
              lineColor: const Color(0xFFF52C56),
              errorLineColor: const Color(0xffDD001B),
              singleLineCount: 3,
              identifySize: 60.0,
              size: 250,
              minLength: 4,
              // answer: type == MinePasswordType.setting ? null : globalStore.password,
              errorItem: Image.asset(
                'error.webp'.mineImgPath,
                color: Color(0xFFF52C56),
              ),
              selectedItem: Image.asset(
                'select.png'.mineImgPath,
                color: Color(0xFFF52C56),
              ),
              normalItem: Image.asset('normal.png'.mineImgPath),
              onComplete: (reslut) async {
                if (reslut.length < 4) {
                  showToast('请至少链接4个点');
                  return;
                }
                if (type == MinePasswordType.setting) {
                  final res = await globalStore.setLockPassword(List<int>.from(reslut));
                  if (res == true) {
                    Get.back();
                  }
                  return;
                }
                if (type == MinePasswordType.close) {
                  final res = await globalStore.closeLockPassword(List<int>.from(reslut));
                  if (res == true) {
                    Get.back();
                  }
                  return;
                }
                final res = await globalStore.checkLockPassword(List<int>.from(reslut));
                if (res == true) {
                  Get.toNamed(SplashPage.routeName);
                }
              },
            ),
          )
        ],
      ),
    );
  }

  String _generateTitle() {
    switch (type) {
      case MinePasswordType.check:
        return '请输入解锁密码';
      case MinePasswordType.setting:
        return '请绘制锁屏图形';
      default:
        return '请绘制锁屏图形';
    }
  }
}
